<?php
namespace app\index\controller;

use app\index\model\ContractCaiModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\StuffCaiListModel;
use app\index\model\StuffCaiModel;
use app\index\model\StuffChuModel;
use app\index\model\StuffJieListModel;
use app\index\model\StuffJieModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\model\StuffTuiModel;
use app\index\model\UserModel;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\StuffService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Stuff extends Common
{
    public function list_stuff_ru(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }
        if(isset($param['danid']) && !empty($param['danid'])){
            $where[] = ['ru.danid','=', $param['danid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|ru.number|cai.name|ru.number|ru.money|ru.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }

        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ru.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['type']) && $param['type'] != ''){
            $where[] = ['ru.type','=', $param['type']];
        }

        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['ru.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['rudt']) && !empty($param['rudt'])){
            $where[] = ['ru.rudt','between', explode(' - ', $param['rudt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ru.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['ru.status','=', $param['status']];
        }
        if(isset($param['ishe']) && $param['ishe'] != ''){
            $where[] = ['ru.ishe','=', $param['ishe']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['ishe'] = (new StuffService())->get_stuff_ru_ishe();
        $data['is_ishe'] = isset($param['ishe']) ? $param['ishe'] : '';

        $whereGonger = '';
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $whereGonger = "(ru.caiid > 0 and cai.gonger like '%".$param['gonger']."%')";
            $whereGonger .= " or (ru.caiid < 1 and ru.gonger like '%".$param['gonger']."%')";
        }

        $data['data'] = StuffRuModel::alias('ru')
            ->field('ru.*,e.name as enginname,cai.name as caiid_name,cai.gonger as caigonger')
            ->join('engin e','e.id=ru.enginid')
            ->leftJoin('contract_cai cai','cai.id=ru.caiid')
            ->where($where)
            ->where($whereGonger)
            ->where((new MenuService())->get_miss_where('stuff_ru', 'ru'))
            ->order('ru.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['caiid'] > 0){
                    $item['gonger'] = $item['caigonger'];
                }

                $item['ishe_name'] = (new StuffService())->get_stuff_ru_ishe_name($item['ishe']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('stuff_ru');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_stuff_ru(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请选择供应商');
            }



            if(empty($param['rudt'])){
                return outputJson(4000, '请选择入库日期');
            }

            $param['number'] = (new StuffService())->create_stuff_ru_number();
            $chu_number = (new StuffService())->create_stuff_chu_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入入库明细');
            }


            Db::startTrans();
            try {
                $id = StuffRuModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['ruid'] = $id;
                    $chuname = $vv['chuname'];
                    $lingname = $vv['lingname'];
                    unset($vv['chuname']);
                    unset($vv['lingname']);
                    $rulistId = StuffRuListModel::insertGetId($vv);

                    if($chuname == '是'){
                        StuffChuModel::insert([
                            'comid'=>$param['comid'],
                            'chanid'=>$param['chanid'],
                            'deptid'=>$param['deptid'],
                            'enginid'=>$param['enginid'],
                            'ruid'=>$rulistId,
                            'number'=>$chu_number,
                            'chudt'=>$param['rudt'],
                            'baner'=>$lingname,
                            'num'=>$vv['num'],
                            'explain'=>$vv['explain'],
                            'created_user'=>$param['created_user'],
                            'created_at'=>$param['created_at'],
                        ]);
                    }



                }

                (new OptService())->relation_file($files,'stuff_ru',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'stuff_ru', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'stuff_ru', $id, '材料入库单');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new StuffService())->create_stuff_ru_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_stuff_ru(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = StuffRuModel::where(['id'=>$param['id']])->find();

            if(empty($param['gonger'])){
                return outputJson(4000, '请选择供应商');
            }

            if(empty($param['rudt'])){
                return outputJson(4000, '请选择入库日期');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入入库明细');
            }

            Db::startTrans();
            try {
                StuffRuModel::update($param);

                StuffRuListModel::where(['ruid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['ruid'] = $param['id'];
                    StuffRuListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'stuff_ru',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'stuff_ru', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'stuff_ru', $param['id'], '材料入库单');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = StuffRuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['caiidinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $list = StuffRuListModel::where(['ruid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('stuff_ru', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_stuff_ru(Request $request){
        $param = $request->post();
        $data = StuffRuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            StuffRuModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'stuff_ru', $data['id'], '材料入库单');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_stuff_ru(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = StuffRuModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['ishe_name'] = (new StuffService())->get_stuff_ru_ishe_name($data['data']['ishe']);

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['danidinfo'] = StuffCaiModel::where(['id'=>$data['data']['danid']])->find();
        $data['caiidinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

        if($data['data']['caiid'] > 0){
            $data['data']['gonger'] = $data['caiidinfo']['gonger'];
        }

        $list = StuffRuListModel::where(['ruid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['data']['leiru'] = 0;
        if($data['data']['caiid'] > 0){
            $data['data']['leiru'] = StuffRuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['enginid'=>$data['data']['enginid'],'caiid'=>$data['data']['caiid'],'isturn'=>1,'status'=>1])->sum('money');
        }



        $data['files'] = (new OptService())->get_file('stuff_ru', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_stuff_cai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['cai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.number|cai.name|cai.number|cai.money|cai.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }

        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['type']) && $param['type'] != ''){
            $where[] = ['cai.type','=', $param['type']];
        }

        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['cai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['caidt']) && !empty($param['caidt'])){
            $where[] = ['cai.caidt','between', explode(' - ', $param['caidt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['cai.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['cai.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';



        $whereGonger = '';
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $whereGonger = "(cai.caiid > 0 and cai.gonger like '%".$param['gonger']."%')";
            $whereGonger .= " or (cai.caiid < 1 and cai.gonger like '%".$param['gonger']."%')";
        }

        $data['data'] = StuffCaiModel::alias('cai')
            ->field('cai.*,e.name as enginname,caihe.name as caiid_name,caihe.gonger as caigonger')
            ->join('engin e','e.id=cai.enginid')
            ->leftJoin('contract_cai caihe','caihe.id=cai.caiid')
            ->where($where)
            ->where($whereGonger)
            ->where((new MenuService())->get_miss_where('stuff_cai', 'cai'))
            ->order('cai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['caiid'] > 0){
                    $item['gonger'] = $item['caigonger'];
                }
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('stuff_cai');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_stuff_cai(Request $request){
        $param = $request->post();
        $data = StuffCaiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            StuffCaiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'stuff_cai', $data['id'], '材料采购单');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_stuff_cai(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = StuffCaiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['caiidinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

        if($data['data']['caiid'] > 0){
            $data['data']['gonger'] = $data['caiidinfo']['gonger'];
        }

        $list = StuffCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['data']['leicai'] = 0;
        if($data['data']['caiid'] > 0){
            $data['data']['leicai'] = StuffCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['enginid'=>$data['data']['enginid'],'caiid'=>$data['data']['caiid'],'isturn'=>1,'status'=>1])->sum('money');
        }

        $data['data']['rukufee'] = StuffRuModel::where(['danid'=>$data['data']['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('money');

        $data['files'] = (new OptService())->get_file('stuff_cai', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_stuff_mingxi_engin(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ru.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['type']) && $param['type'] != ''){
            $where[] = ['ru.type','=', $param['type']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['ru.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['ru.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['rudt']) && !empty($param['rudt'])){
            $where[] = ['ru.rudt','between', explode(' - ', $param['rudt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ru.baner','like', '%'.$param['baner'].'%'];
        }

        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listunit']) && !empty($param['listunit'])){
            $where[] = ['list.unit','like', '%'.$param['listunit'].'%'];
        }
        if(isset($param['listprice']) && !empty($param['listprice'])){
            $where[] = ['list.price','like', '%'.$param['listprice'].'%'];
        }


        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = StuffRuListModel::alias('list')
            ->field('ru.*,e.name as enginname,list.id as listid,list.name as listname,list.unit as listunit
            ,list.price as listprice,list.num as listnum,list.money as listmoney,cai.gonger as caigonger')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->leftJoin('contract_cai cai','cai.id=ru.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('stuff_mingxi', 'ru'))
            ->order('ru.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['caiid'] > 0){
                    $item['gonger'] = $item['caigonger'];
                }
                $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
                $item['chunum'] = StuffChuModel::where(['enginid'=>$item['enginid'],'ruid'=>$item['listid']])->sum('num');
                $item['tuinum'] = StuffTuiModel::where(['enginid'=>$item['enginid'],'ruid'=>$item['listid']])->sum('num');
                $item['cunnum'] = $item['listnum'] - $item['chunum'] + $item['tuinum'];
                $item['cunmoney'] = $item['cunnum'] * $item['listprice'];

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('stuff_mingxi');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_stuff_mingxi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|ru.number|ru.gonger|list.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ru.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['type']) && $param['type'] != ''){
            $where[] = ['ru.type','=', $param['type']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['ru.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['ru.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['rudt']) && !empty($param['rudt'])){
            $where[] = ['ru.rudt','between', explode(' - ', $param['rudt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ru.baner','like', '%'.$param['baner'].'%'];
        }

        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listunit']) && !empty($param['listunit'])){
            $where[] = ['list.unit','like', '%'.$param['listunit'].'%'];
        }
        if(isset($param['listprice']) && !empty($param['listprice'])){
            $where[] = ['list.price','like', '%'.$param['listprice'].'%'];
        }


        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = StuffRuListModel::alias('list')
            ->field('ru.*,e.name as enginname,list.id as listid,list.name as listname,list.unit as listunit
            ,list.price as listprice,list.num as listnum,list.money as listmoney,cai.gonger as caigonger')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->leftJoin('contract_cai cai','cai.id=ru.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('stuff_mingxi', 'ru'))
            ->order('ru.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['caiid'] > 0){
                    $item['gonger'] = $item['caigonger'];
                }
                $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
                $item['chunum'] = StuffChuModel::where(['enginid'=>$item['enginid'],'ruid'=>$item['listid']])->sum('num');
                $item['tuinum'] = StuffTuiModel::where(['enginid'=>$item['enginid'],'ruid'=>$item['listid']])->sum('num');
                $item['cunnum'] = round($item['listnum'] - $item['chunum'] + $item['tuinum'], 2);
                $item['cunmoney'] = round($item['cunnum'] * $item['listprice'], 2);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('stuff_mingxi');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function detail_stuff_mingxi(Request $request){
        $param = $request->post();

        $data = [];

        $listinfo = StuffRuListModel::where(['id'=>$param['ruid']])->find();
        $ruinfo = StuffRuModel::where(['id'=>$listinfo['ruid']])->find();
        $enginname = EnginModel::where(['id'=>$ruinfo['enginid']])->value('name');
        $caiid_name = ContractCaiModel::where(['id'=>$ruinfo['caiid']])->value('name');

        $init = [
            'type' => 'ru',
            'id' => $listinfo['id'],
            'number' => $ruinfo['number'],
            'dt' => $ruinfo['rudt'],
            'dongname' => '入库',
            'name' => $listinfo['name'],
            'unit' => $listinfo['unit'],
            'price' => $listinfo['price'],
            'num' => $listinfo['num'],
            'money' => $listinfo['money'],
            'baner' => $ruinfo['baner'],
            'gonger' => $ruinfo['gonger'],
            'enginname' => $enginname,
            'caiid_name' => $caiid_name,
        ];

        $data[] = $init;


        $other = [];
        $chu = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$listinfo['id']])->select()->toArray();
        foreach ($chu as $v){
            $init_chu = $init;
            $init_chu['type'] = 'chu';
            $init_chu['id'] = $v['id'];
            $init_chu['number'] = $v['number'];
            $init_chu['dt'] = $v['chudt'];
            $init_chu['dongname'] = '出库';
            $init_chu['num'] = $v['num'];
            $init_chu['money'] = $init_chu['price']*$v['num'];
            $init_chu['baner'] = $v['baner'];
            $other[] = $init_chu;
        }

        $tui = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$listinfo['id']])->select()->toArray();
        foreach ($tui as $v){
            $init_chu = $init;
            $init_chu['type'] = 'tui';
            $init_chu['id'] = $v['id'];
            $init_chu['number'] = $v['number'];
            $init_chu['dt'] = $v['tuidt'];
            $init_chu['dongname'] = '退库';
            $init_chu['num'] = $v['num'];
            $init_chu['money'] = $init_chu['price']*$v['num'];
            $init_chu['baner'] = $v['baner'];
            $other[] = $init_chu;
        }


        $dtSort = array_column($other,'dt');
        array_multisort($dtSort,SORT_ASC,$other);

        foreach ($other as $vvv){
            $data[] = $vvv;
        }


        return outputJson(2000,lang('code_2000'),$data);
    }
    public function chu_stuff_mingxi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }

            if(empty($param['chudt'])){
                return outputJson(4000, '请选择出库日期');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['deptid'] = $this->userinfo['deptid'];
            $param['number'] = (new StuffService())->create_stuff_chu_number();
            $param['created_user'] = $this->userinfo['id'];
            $param['created_at'] = date('Y-m-d H:i:s');



            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入出库明细');
            }


            foreach ($list as &$item){
                $item['comid'] = $param['comid'];
                $item['chanid'] = $param['chanid'];
                $item['deptid'] = $param['deptid'];
                $item['enginid'] = $param['enginid'];
                $item['number'] = $param['number'];
                $item['chudt'] = $param['chudt'];
                $item['baner'] = $param['baner'];
                $item['created_user'] = $param['created_user'];
                $item['created_at'] = $param['created_at'];
            }


            Db::startTrans();
            try {
                foreach($list as $v){
                    StuffChuModel::insert($v);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $ruid = $request->param('ruid', 0, 'int');

            $rulistinfo = StuffRuListModel::where(['id'=>$ruid])->find();
            if($rulistinfo){
                $ruinfo = StuffRuModel::where(['id'=>$rulistinfo['ruid']])->find();
                $data['engininfo'] = EnginModel::get_info_by_pk($ruinfo['enginid']);

                $chunum = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $tuinum = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');


                $cunnum = round($rulistinfo['num'] - $chunum + $tuinum, 3);
                $data['list'] = [
                    'ruid' => $rulistinfo['id'],
                    'name' => $rulistinfo['name'],
                    'unit' => $rulistinfo['unit'],
                    'gonger' => $ruinfo['gonger'],
                    'runum' => $rulistinfo['num'],
                    'chunum' => $chunum,
                    'cunnum' => $cunnum,
                ];
            }

            $data['number'] = (new StuffService())->create_stuff_chu_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function chu_edit_stuff_mingxi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入出库明细');
            }

            Db::startTrans();
            try {
                foreach($list as $v){
                    StuffChuModel::where(['id'=>$v['id']])->update([
                        'num' => $v['num'],
                        'explain' => $v['explain'],
                    ]);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->param('id', 0, 'int');

            $data['data'] = StuffChuModel::where(['id'=>$id])->find();
            $rulistinfo = StuffRuListModel::where(['id'=>$data['data']['ruid']])->find();
            if($rulistinfo){
                $ruinfo = StuffRuModel::where(['id'=>$rulistinfo['ruid']])->find();
                $data['engininfo'] = EnginModel::get_info_by_pk($ruinfo['enginid']);

                $chunum = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $tuinum = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $cunnum = $rulistinfo['num'] - $chunum + $tuinum;

                $data['list'] = [
                    'id' => $data['data']['id'],
                    'ruid' => $rulistinfo['id'],
                    'name' => $rulistinfo['name'],
                    'unit' => $rulistinfo['unit'],
                    'gonger' => $ruinfo['gonger'],
                    'runum' => $rulistinfo['num'],
                    'chunum' => $chunum,
                    'cunnum' => $cunnum,
                    'num' => $data['data']['num'],
                    'explain' => $data['data']['explain'],
                ];
            }

            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function tui_stuff_mingxi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }

            if(empty($param['tuidt'])){
                return outputJson(4000, '请选择退库日期');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['deptid'] = $this->userinfo['deptid'];
            $param['number'] = (new StuffService())->create_stuff_tui_number();
            $param['created_user'] = $this->userinfo['id'];
            $param['created_at'] = date('Y-m-d H:i:s');



            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入退库明细');
            }


            foreach ($list as &$item){
                $item['comid'] = $param['comid'];
                $item['chanid'] = $param['chanid'];
                $item['deptid'] = $param['deptid'];
                $item['enginid'] = $param['enginid'];
                $item['number'] = $param['number'];
                $item['tuidt'] = $param['tuidt'];
                $item['baner'] = $param['baner'];
                $item['created_user'] = $param['created_user'];
                $item['created_at'] = $param['created_at'];
            }


            Db::startTrans();
            try {
                foreach($list as $v){
                    StuffTuiModel::insert($v);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $ruid = $request->param('ruid', 0, 'int');

            $rulistinfo = StuffRuListModel::where(['id'=>$ruid])->find();
            if($rulistinfo){
                $ruinfo = StuffRuModel::where(['id'=>$rulistinfo['ruid']])->find();
                $data['engininfo'] = EnginModel::get_info_by_pk($ruinfo['enginid']);

                $chunum = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $tuinum = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $cunnum = round($rulistinfo['num'] - $chunum + $tuinum, 3);
                $data['list'] = [
                    'ruid' => $rulistinfo['id'],
                    'name' => $rulistinfo['name'],
                    'unit' => $rulistinfo['unit'],
                    'gonger' => $ruinfo['gonger'],
                    'runum' => $rulistinfo['num'],
                    'chunum' => $chunum,
                    'tuinum' => $tuinum,
                    'cunnum' => $cunnum,
                ];
            }

            $data['number'] = (new StuffService())->create_stuff_tui_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function tui_edit_stuff_mingxi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入退库明细');
            }

            Db::startTrans();
            try {
                foreach($list as $v){
                    StuffTuiModel::where(['id'=>$v['id']])->update([
                        'num' => $v['num'],
                        'explain' => $v['explain'],
                    ]);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->param('id', 0, 'int');

            $data['data'] = StuffTuiModel::where(['id'=>$id])->find();
            $rulistinfo = StuffRuListModel::where(['id'=>$data['data']['ruid']])->find();
            if($rulistinfo){
                $ruinfo = StuffRuModel::where(['id'=>$rulistinfo['ruid']])->find();
                $data['engininfo'] = EnginModel::get_info_by_pk($ruinfo['enginid']);

                $chunum = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $tuinum = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $cunnum = $rulistinfo['num'] - $chunum + $tuinum;

                $data['list'] = [
                    'id' => $data['data']['id'],
                    'ruid' => $rulistinfo['id'],
                    'name' => $rulistinfo['name'],
                    'unit' => $rulistinfo['unit'],
                    'gonger' => $ruinfo['gonger'],
                    'runum' => $rulistinfo['num'],
                    'chunum' => $chunum,
                    'tuinum' => $tuinum,
                    'num' => $data['data']['num'],
                    'explain' => $data['data']['explain'],
                ];
            }

            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function chuji_stuff_chu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['chu.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['ru.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['chu.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['chudt']) && !empty($param['chudt'])){
            $where[] = ['chu.chudt','between', explode(' - ', $param['chudt'])];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['chu.baner','like', '%'.$param['baner'].'%'];
        }


        $data['data'] = StuffChuModel::alias('chu')
            ->field('chu.*,e.name as enginname,cai.name as cainame,ru.gonger,list.name as listname,list.unit as listunit
            ,list.price as listprice,ru.caiid')
            ->join('stuff_ru_list list','list.id=chu.ruid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->leftJoin('contract_cai cai','cai.id=ru.caiid')
            ->where($where)
            ->order('ru.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['listmoney'] = $item['listprice'] * $item['num'];
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function mingxi_stuff_chu222(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['chu.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['ru.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['chu.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['chudt']) && !empty($param['chudt'])){
            $where[] = ['chu.chudt','between', explode(' - ', $param['chudt'])];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['chu.baner','like', '%'.$param['baner'].'%'];
        }


        $data['data'] = StuffChuModel::alias('chu')
            ->field('chu.*,e.name as enginname,cai.name as cainame,ru.gonger,ru.caiid')
            ->join('stuff_ru_list list','list.id=chu.ruid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->leftJoin('contract_cai cai','cai.id=ru.caiid')
            ->where($where)
            ->group('chu.number')
            ->order('chu.number DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_stuff_danchu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['chu.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['chu.number|e.name|chu.baner','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['chu.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['chu.baner','like', '%'.$param['baner'].'%'];
        }

        $data['data'] = StuffChuModel::alias('chu')
            ->field('chu.*,e.name as enginname')
            ->join('engin e','e.id=chu.enginid')
            ->join('stuff_ru_list list','list.id=chu.ruid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->where($where)
            ->group('chu.number')
            ->order('chu.number DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_stuff_dantui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['tui.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['tui.number|e.name|tui.baner','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['tui.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['tui.baner','like', '%'.$param['baner'].'%'];
        }

        $data['data'] = StuffTuiModel::alias('tui')
            ->field('tui.*,e.name as enginname')
            ->join('engin e','e.id=tui.enginid')
            ->join('stuff_ru_list list','list.id=tui.ruid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->where($where)
            ->group('tui.number')
            ->order('tui.number DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }



    public function list_stuff_chu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['chu.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['chu.number|e.name|chu.baner','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['chu.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['chu.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chudt']) && !empty($param['chudt'])){
            $where[] = ['chu.chudt','between', explode(' - ', $param['chudt'])];
        }
        if(isset($param['created_user_name']) && !empty($param['created_user_name'])){
            $where[] = ['u.realname','like', '%'.$param['created_user_name'].'%'];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';



        $data['data'] = StuffChuModel::alias('chu')
            ->field('chu.*,e.name as enginname,u.realname as created_user_name')
            ->join('engin e','e.id=chu.enginid')
            ->join('stuff_ru_list list','list.id=chu.ruid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('user u','u.id=chu.created_user')
            ->where($where)
            ->where((new MenuService())->get_miss_where('stuff_ru', 'ru'))
            ->group('chu.number')
            ->order('chu.number DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('stuff_chu');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_stuff_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }

            if(empty($param['chudt'])){
                return outputJson(4000, '请选择出库日期');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['deptid'] = $this->userinfo['deptid'];
            $param['number'] = (new StuffService())->create_stuff_chu_number();
            $param['created_user'] = $this->userinfo['id'];
            $param['created_at'] = date('Y-m-d H:i:s');



            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入出库明细');
            }


            foreach ($list as &$item){
                $item['comid'] = $param['comid'];
                $item['chanid'] = $param['chanid'];
                $item['deptid'] = $param['deptid'];
                $item['enginid'] = $param['enginid'];
                $item['number'] = $param['number'];
                $item['chudt'] = $param['chudt'];
                $item['baner'] = $param['baner'];
                $item['created_user'] = $param['created_user'];
                $item['created_at'] = $param['created_at'];
            }


            Db::startTrans();
            try {
                foreach($list as $v){
                    StuffChuModel::insert($v);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new StuffService())->create_stuff_chu_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_stuff_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入出库明细');
            }

            $chuinfo = StuffChuModel::where(['id'=>$param['id']])->find();

            $param['comid'] = $chuinfo['comid'];
            $param['chanid'] = $chuinfo['chanid'];
            $param['deptid'] = $chuinfo['deptid'];
            $param['enginid'] = $chuinfo['enginid'];
            $param['number'] = $chuinfo['number'];
            $param['created_user'] = $chuinfo['created_user'];
            $param['created_at'] = $chuinfo['created_at'];
            Db::startTrans();
            try {

                StuffChuModel::where(['number'=>$param['number']])->delete();
                foreach($list as $v){
                    StuffChuModel::insert([
                        'comid' => $param['comid'],
                        'chanid' => $param['chanid'],
                        'deptid' => $param['deptid'],
                        'enginid' => $param['enginid'],
                        'number' => $param['number'],
                        'created_user' => $param['created_user'],
                        'created_at' => $param['created_at'],
                        'chudt' => $param['chudt'],
                        'baner' => $param['baner'],
                        'ruid' => $v['ruid'],
                        'num' => $v['num'],
                        'explain' => $v['explain'],
                    ]);
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->param('id', 0, 'int');
            $data['data'] = StuffChuModel::where(['id'=>$id])->find();


            $engininfo = EnginModel::where(['id'=>$data['data']['enginid']])->find();
            $data['engininfo'] = $engininfo;


            $list = StuffChuModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'number'=>$data['data']['number']])->select()->toArray();
            foreach ($list as &$item){
                $rulistinfo = StuffRuListModel::where(['id'=>$item['ruid']])->find();
                $ruinfo = StuffRuModel::where(['id'=>$rulistinfo['ruid']])->find();
                $chunum = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $chunum -= $item['num'];

                $tuinum = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $cunnum = $rulistinfo['num'] - $chunum + $tuinum;

                $item['name'] = $rulistinfo['name'];
                $item['unit'] = $rulistinfo['unit'];
                $item['gonger'] = $ruinfo['gonger'];
                $item['runum'] = $rulistinfo['num'];
                $item['chunum'] = $chunum;
                $item['cunnum'] = $cunnum;
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function del_stuff_chu(Request $request){
        $param = $request->post();
        $data = StuffChuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            StuffChuModel::where(['id'=>$data['id']])->delete();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_stuff_chu(Request $request){
        $id = $request->param('id', 0, 'int');
        $data['data'] = StuffChuModel::where(['id'=>$id])->find();


        $engininfo = EnginModel::where(['id'=>$data['data']['enginid']])->find();
        $data['engininfo'] = $engininfo;


        $list = StuffChuModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'number'=>$data['data']['number']])->select()->toArray();
        foreach ($list as &$item){
            $rulistinfo = StuffRuListModel::where(['id'=>$item['ruid']])->find();
            $ruinfo = StuffRuModel::where(['id'=>$rulistinfo['ruid']])->find();
            $chunum = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
            $chunum -= $item['num'];

            $tuinum = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
            $cunnum = $rulistinfo['num'] - $chunum + $tuinum;

            $item['name'] = $rulistinfo['name'];
            $item['unit'] = $rulistinfo['unit'];
            $item['gonger'] = $ruinfo['gonger'];
            $item['runum'] = $rulistinfo['num'];
            $item['chunum'] = $chunum;
            $item['cunnum'] = $cunnum;
        }
        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_stuff_tui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['tui.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['tui.number|e.name|tui.baner','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['tui.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['tui.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['tuidt']) && !empty($param['tuidt'])){
            $where[] = ['tui.tuidt','between', explode(' - ', $param['tuidt'])];
        }
        if(isset($param['created_user_name']) && !empty($param['created_user_name'])){
            $where[] = ['u.realname','like', '%'.$param['created_user_name'].'%'];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';



        $data['data'] = StuffTuiModel::alias('tui')
            ->field('tui.*,e.name as enginname,u.realname as created_user_name')
            ->join('engin e','e.id=tui.enginid')
            ->join('stuff_ru_list list','list.id=tui.ruid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('user u','u.id=tui.created_user')
            ->where($where)
            ->where((new MenuService())->get_miss_where('stuff_ru', 'ru'))
            ->group('tui.number')
            ->order('tui.number DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('stuff_tui');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_stuff_tui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }

            if(empty($param['tuidt'])){
                return outputJson(4000, '请选择退库日期');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['deptid'] = $this->userinfo['deptid'];
            $param['number'] = (new StuffService())->create_stuff_tui_number();
            $param['created_user'] = $this->userinfo['id'];
            $param['created_at'] = date('Y-m-d H:i:s');



            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入退库明细');
            }


            foreach ($list as &$item){
                $item['comid'] = $param['comid'];
                $item['chanid'] = $param['chanid'];
                $item['deptid'] = $param['deptid'];
                $item['enginid'] = $param['enginid'];
                $item['number'] = $param['number'];
                $item['tuidt'] = $param['tuidt'];
                $item['baner'] = $param['baner'];
                $item['created_user'] = $param['created_user'];
                $item['created_at'] = $param['created_at'];
            }



            Db::startTrans();
            try {
                foreach($list as $v){
                    StuffTuiModel::insert($v);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new StuffService())->create_stuff_tui_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_stuff_tui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入退库明细');
            }


            $tuiinfo = StuffTuiModel::where(['id'=>$param['id']])->find();

            $param['comid'] = $tuiinfo['comid'];
            $param['chanid'] = $tuiinfo['chanid'];
            $param['deptid'] = $tuiinfo['deptid'];
            $param['enginid'] = $tuiinfo['enginid'];
            $param['number'] = $tuiinfo['number'];
            $param['created_user'] = $tuiinfo['created_user'];
            $param['created_at'] = $tuiinfo['created_at'];

            Db::startTrans();
            try {

                StuffTuiModel::where(['number'=>$param['number']])->delete();
                foreach($list as $v){
                    StuffTuiModel::insert([
                        'comid' => $param['comid'],
                        'chanid' => $param['chanid'],
                        'deptid' => $param['deptid'],
                        'enginid' => $param['enginid'],
                        'number' => $param['number'],
                        'created_user' => $param['created_user'],
                        'created_at' => $param['created_at'],
                        'tuidt' => $param['tuidt'],
                        'baner' => $param['baner'],
                        'ruid' => $v['ruid'],
                        'num' => $v['num'],
                        'explain' => $v['explain'],
                    ]);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->param('id', 0, 'int');
            $data['data'] = StuffTuiModel::where(['id'=>$id])->find();


            $engininfo = EnginModel::where(['id'=>$data['data']['enginid']])->find();
            $data['engininfo'] = $engininfo;


            $list = StuffTuiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'number'=>$data['data']['number']])->select()->toArray();
            foreach ($list as &$item){
                $rulistinfo = StuffRuListModel::where(['id'=>$item['ruid']])->find();
                $ruinfo = StuffRuModel::where(['id'=>$rulistinfo['ruid']])->find();
                $chunum = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $tuinum = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $tuinum -= $item['num'];


                $item['name'] = $rulistinfo['name'];
                $item['unit'] = $rulistinfo['unit'];
                $item['gonger'] = $ruinfo['gonger'];
                $item['runum'] = $rulistinfo['num'];
                $item['chunum'] = $chunum;
                $item['tuinum'] = $tuinum;
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function del_stuff_tui(Request $request){
        $param = $request->post();
        $data = StuffTuiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            StuffTuiModel::where(['id'=>$data['id']])->delete();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_stuff_tui(Request $request){
        $id = $request->param('id', 0, 'int');
        $data['data'] = StuffTuiModel::where(['id'=>$id])->find();


        $engininfo = EnginModel::where(['id'=>$data['data']['enginid']])->find();
        $data['engininfo'] = $engininfo;


        $list = StuffTuiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'number'=>$data['data']['number']])->select()->toArray();
        foreach ($list as &$item){
            $rulistinfo = StuffRuListModel::where(['id'=>$item['ruid']])->find();
            $ruinfo = StuffRuModel::where(['id'=>$rulistinfo['ruid']])->find();
            $chunum = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
            $tuinum = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');

            $item['name'] = $rulistinfo['name'];
            $item['unit'] = $rulistinfo['unit'];
            $item['gonger'] = $ruinfo['gonger'];
            $item['runum'] = $rulistinfo['num'];
            $item['chunum'] = $chunum;
            $item['tuinum'] = $tuinum;
        }
        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_stuff_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|jie.number|jie.gonger|jie.money|jie.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jie.enginid','=', $param['enginid']];
        }
        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where[] = ['jie.caiid','=', $param['caiid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['jie.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jie.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['jiedt']) && !empty($param['jiedt'])){
            $where[] = ['jie.jiedt','between', explode(' - ', $param['jiedt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jie.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = StuffJieModel::alias('jie')
            ->field('jie.*,e.name as enginname')
            ->join('engin e','e.id=jie.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('stuff_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('stuff_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_stuff_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['zhuname'])){
                return outputJson(4000, '请输入结算主题');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请选择供应商');
            }

            if(empty($param['jiedt'])){
                return outputJson(4000, '请选择结算日期');
            }

            $param['number'] = (new StuffService())->create_stuff_jie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入结算明细');
            }


            Db::startTrans();
            try {
                $id = StuffJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    StuffJieListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'stuff_jie',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'stuff_jie', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'stuff_jie', $id, '材料结算单');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new StuffService())->create_stuff_jie_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_stuff_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = StuffJieModel::where(['id'=>$param['id']])->find();


            if(empty($param['zhuname'])){
                return outputJson(4000, '请输入结算主题');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请选择供应商');
            }

            if(empty($param['jiedt'])){
                return outputJson(4000, '请选择结算日期');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入结算明细');
            }

            Db::startTrans();
            try {
                StuffJieModel::update($param);

                StuffJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    StuffJieListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'stuff_jie',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'stuff_jie', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'stuff_jie', $param['id'], '材料结算单');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = StuffJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['caiidinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

            $list = StuffJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('stuff_jie', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_stuff_jie(Request $request){
        $param = $request->post();
        $data = StuffJieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            StuffJieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'stuff_jie', $data['id'], '材料结算单');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_stuff_jie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = StuffJieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = StuffJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['caiidinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

        $data['files'] = (new OptService())->get_file('stuff_jie', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_stuff_ku(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }

        $list = [];
        $info = StuffRuListModel::alias('list')
            ->field('list.name,list.unit,sum(list.num) as listnum,sum(list.money) as listmoney
            ,GROUP_CONCAT(list.id) as idstr,GROUP_CONCAT(list.price) as pricestr,ru.enginid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('stuff_ku', 'ru'))
            ->order('list.name ASC')
            ->group('list.name,list.unit')
            ->select()->toArray();

        foreach ($info as &$item){
            $item['enginid_name'] = EnginModel::where(['id'=>$item['enginid']])->value('name');

            $idArr = explode(',', $item['idstr']);
            $priceArr = explode(',', $item['pricestr']);

            $item['chu_num'] = 0;
            $item['chu_money'] = 0;
            foreach ($idArr as $k=>$ruid){
                $num = StuffChuModel::where(['ruid'=>$ruid])->sum('num');
                $money = $num*$priceArr[$k];
                $item['chu_num'] += $num;
                $item['chu_money'] += $money;
            }

            $item['tui_num'] = 0;
            $item['tui_money'] = 0;
            foreach ($idArr as $k=>$ruid){
                $num = StuffTuiModel::where(['ruid'=>$ruid])->sum('num');
                $money = $num*$priceArr[$k];
                $item['tui_num'] += $num;
                $item['tui_money'] += $money;
            }


            $item['hao_num'] = $item['chu_num'] - $item['tui_num'];
            $item['hao_money'] = $item['chu_money'] - $item['tui_money'];


            $item['ku_num'] = $item['listnum'] - $item['chu_num'] + $item['tui_num'];
            $item['ku_money'] = $item['listmoney'] - $item['chu_money'] + $item['tui_money'];

            $item['hao_num'] = round($item['hao_num'], 2);
            $item['hao_money'] = round($item['hao_money'], 2);
            $item['ku_num'] = round($item['ku_num'], 2);
            $item['ku_money'] = round($item['ku_money'], 2);


            $item['chu_num'] = round($item['chu_num'], 2);
            $item['chu_money'] = round($item['chu_money'], 2);
            $item['tui_num'] = round($item['tui_num'], 2);
            $item['tui_money'] = round($item['tui_money'], 2);
            $item['hao_num'] = round($item['hao_num'], 2);
            $item['hao_money'] = round($item['hao_money'], 2);
            $item['ku_num'] = round($item['ku_num'], 2);
            $item['ku_money'] = round($item['ku_money'], 2);


            if(isset($param['isku']) && $param['isku'] == 1){
                if($item['ku_num'] > 0){
                    $list[] = $item;
                }
            }elseif(isset($param['isku']) && $param['isku'] == 2){
                if($item['ku_num'] <= 0){
                    $list[] = $item;
                }
            }else{
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function detail_stuff_ku(Request $request){
        $param = $request->post();
        $chatype = $request->post('chatype', 0, 'int');

        $data = [];

        $listinfo = StuffRuListModel::where('id','in',$param['idstr'])->select()->toArray();
        foreach ($listinfo as $item){
            $ruinfo = StuffRuModel::where(['id'=>$item['ruid']])->find();
            $enginname = EnginModel::where(['id'=>$ruinfo['enginid']])->value('name');
            $caiid_name = ContractCaiModel::where(['id'=>$ruinfo['caiid']])->value('name');

            $init = [
                'type' => 'ru',
                'id' => $item['id'],
                'number' => $ruinfo['number'],
                'dt' => $ruinfo['rudt'],
                'dongname' => '入库',
                'name' => $item['name'],
                'unit' => $item['unit'],
                'price' => $item['price'],
                'num' => $item['num'],
                'money' => $item['money'],
                'baner' => $ruinfo['baner'],
                'gonger' => $ruinfo['gonger'],
                'enginname' => $enginname,
                'caiid_name' => $caiid_name,
            ];
            if($chatype == 0 || $chatype == 1){
                $data[] = $init;
            }



            if($chatype == 0 || $chatype == 2){
                $chu = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$item['id']])->select()->toArray();
                foreach ($chu as $v){
                    $init_chu = $init;
                    $init_chu['type'] = 'chu';
                    $init_chu['id'] = $v['id'];
                    $init_chu['number'] = $v['number'];
                    $init_chu['dt'] = $v['chudt'];
                    $init_chu['dongname'] = '出库';
                    $init_chu['num'] = $v['num'];
                    $init_chu['money'] = $init_chu['price']*$v['num'];
                    $init_chu['baner'] = $v['baner'];
                    $data[] = $init_chu;
                }
            }


            if($chatype == 0 || $chatype == 3){
                $tui = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$item['id']])->select()->toArray();
                foreach ($tui as $v){
                    $init_chu = $init;
                    $init_chu['type'] = 'tui';
                    $init_chu['id'] = $v['id'];
                    $init_chu['number'] = $v['number'];
                    $init_chu['dt'] = $v['tuidt'];
                    $init_chu['dongname'] = '退库';
                    $init_chu['num'] = $v['num'];
                    $init_chu['money'] = $init_chu['price']*$v['num'];
                    $init_chu['baner'] = $v['baner'];
                    $data[] = $init_chu;
                }
            }



        }

        $dtSort = array_column($data,'dt');
        array_multisort($dtSort,SORT_ASC,$data);

        return outputJson(2000,lang('code_2000'),$data);
    }






    public function mingxi_stuff_ru(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && $param['enginid'] != ''){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['ru.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ru.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['rudt']) && !empty($param['rudt'])){
            $where[] = ['ru.rudt','between', explode(' - ', $param['rudt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ru.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['ishe']) && $param['ishe'] != ''){
            $where[] = ['ru.ishe','=', $param['ishe']];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['ishe'] = (new StuffService())->get_stuff_ru_ishe();
        $data['is_ishe'] = isset($param['ishe']) ? $param['ishe'] : '';


        $data['data'] = StuffRuListModel::alias('list')
            ->field('ru.*,e.name as enginname,list.id as listid,list.name as listname,list.unit as listunit
            ,list.price as listprice,list.num as listnum,list.money as listmoney')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('mingxi_stuff_ru', 'ru'))
            ->order('ru.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['he_name'] = '';
                if($item['ishe'] == 1){
                    $item['he_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
                }elseif($item['ishe'] == 2){
                    $item['caiid'] = StuffCaiModel::where(['id'=>$item['danid']])->value('caiid');
                    $item['he_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
                }
                $item['ishe_name'] = (new StuffService())->get_stuff_ru_ishe_name($item['ishe']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function mingxi_stuff_chu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && $param['enginid'] != ''){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['chu.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['chu.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chudt']) && !empty($param['chudt'])){
            $where[] = ['chu.chudt','between', explode(' - ', $param['chudt'])];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';



        $data['data'] = StuffChuModel::alias('chu')
            ->field('chu.*,e.name as enginname,list.name as listname,list.unit as listunit,ru.gonger')
            ->join('stuff_ru_list list','list.id=chu.ruid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('mingxi_stuff_chu', 'ru'))
            ->order('chu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function mingxi_stuff_tui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && $param['enginid'] != ''){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['tui.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['tui.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['tuidt']) && !empty($param['tuidt'])){
            $where[] = ['tui.tuidt','between', explode(' - ', $param['tuidt'])];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';



        $data['data'] = StuffTuiModel::alias('tui')
            ->field('tui.*,e.name as enginname,list.name as listname,list.unit as listunit,ru.gonger')
            ->join('stuff_ru_list list','list.id=tui.ruid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('mingxi_stuff_tui', 'ru'))
            ->order('tui.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


















}
